import Vue from 'vue'
import axios from 'axios'
import qs from 'qs'
import { Message, Loading } from 'element-ui'

// 响应时间
axios.defaults.timeout = 5 * 1000
// 配置cookie
// axios.defaults.withCredentials = true
// 配置请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 静态资源
Vue.prototype.$static = ''

// 配置接口地址
axios.defaults.baseURL = ''
var loadingInstance
// POST传参序列化(添加请求拦截器)
axios.interceptors.request.use(
    config => {
        loadingInstance = Loading.service({
            lock: true,
            text: '数据加载中，请稍后...',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
        })
         // 若是有做鉴权token , 就给头部带上token
        if (localStorage.token) {
            config.headers.Authorization = localStorage.token;
        }
        if (config.method === "post" ||
            config.method === "put" ) {
            config.data = qs.stringify(config.data)
        }
        return config
    },
    err => {
        loadingInstance.close()
        Message.error('请求错误')
        return Promise.reject(err)
    }
)
// 返回状态判断(添加响应拦截器)
axios.interceptors.response.use(
    res => {
        loadingInstance.close();
        if (res.data.code === 200 ) {
            return res.data.data;
        } else {
            Message.error(res.data.message)
            return Promise.reject(res.data)
        }
    },
    err => {
        loadingInstance.close()
        Message.error('远程服务失败')
        return Promise.reject(err)
    }
)

//get请求
export  function get(url) {
    return body => axios.get(url, { params: body })
  }
//post请求
export function post(url) {
    return body => axios.post(url, body)
}


//export default axios;
// 发送post请求
/*
export function post(url, params) {
    return new Promise((resolve, reject) => {
        axios
            .post(url, params)
            .then(
                res => {
                    resolve(res.data)
                },
                err => {
                    reject(err.data)
                }
            )
            .catch(err => {
                reject(err.data)
            })
    })
}
// 发送put请求
export function put(url, params) {
    return new Promise((resolve, reject) => {
        axios
            .put(url, params)
            .then(
                res => {
                    resolve(res.data)
                },
                err => {
                    reject(err.data)
                }
            )
            .catch(err => {
                reject(err.data)
            })
    })
}

// export function get(url, params) {
//     return new Promise((resolve, reject) => {
//         axios
//             .get(url, {
//                 params: params
//             })
//             .then(res => {
//                 resolve(res.data)
//             })
//             .catch(err => {
//                 reject(err.data)
//             })
//     })
// }

export function get(url, params) {
    return new Promise((resolve, reject) => {
        axios
            .get(url,  params
            ).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err.data)
            })
    })
}


export function del(url, params) {
    return new Promise((resolve=defaultRun, reject=defaultErr) => {
        axios
            .delete(url, {
                params: params
            })
            .then(res => {
                resolve(res.data)
            })
            .catch(err => {
                reject(err.data)
            })
    })
}
*/
